﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewFormatProject.Server.Instances;
using NewFormatProject.Server.Interfaces;
using NewFormatProject.Api.Admin.Filters;
using System.Threading.Tasks;
using NewFormatProject.Infrastructure.Atrributes;
using System;
using Microsoft.AspNetCore.Authorization;
using NewFormatProject.Infrastructure.Dto.Responses;
using NewFormatProject.Infrastructure.Dto.Requests;

namespace NewFormatProject.Api.Admin.Controllers
{
    public class LoginController : BaseController
    {
        private readonly ILoginServer _loginServer;
        public LoginController(ILoginServer loginServer) {
            _loginServer = loginServer;
        }
        
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Login(RequestLoginDto dto)
        {
            return Ok(await _loginServer.Login(dto));
        }
        /// <summary>
        /// 获取登录日志
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [CheckLogin]
        public async Task<IActionResult> GetLoginLog(int page,int limit)
        {
            var uid= User.FindFirst("uid")?.Value;
            if (uid == null)
            {
                return BadRequest();
            }
            return Ok(await _loginServer.GetLoginLog(Convert.ToInt32(uid),page,limit));
        }
        /// <summary>
        /// 检查登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [CheckLogin]
        public  IActionResult CheckLogin()
        {
            return Ok(ResponseApiDto.Success("已登录",new
            {
                id=_user.UId,
                avatar=_user.UAvatar,
                name=_user.UName
            }));
        }
        /// <summary>
        /// 生成测试用户
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<IActionResult> GenerateTestUsers()
        {
            return Ok(await _loginServer.GenerateTestUsers());
        }
    }
}
